package com.fashare.activitytracker;

//import android.accessibilityservice.AccessibilityService;
//import android.content.Intent;
//import android.text.TextUtils;
//import android.util.Log;
//import android.view.accessibility.AccessibilityEvent;
//
//import com.fashare.activitytracker.TrackerWindowManager;
//import de.greenrobot.event.EventBus;

import com.fashare.activitytracker.util.TextUtils;
import ohos.aafwk.content.Intent;
import ohos.accessibility.AccessibilityEventInfo;
import ohos.accessibility.ability.AccessibleAbility;
import ohos.multimodalinput.event.KeyEvent;
import org.greenrobot.eventbus.EventBus;

public class TrackerService extends AccessibleAbility {
    public static final String TAG = "TrackerService";
    public static final String COMMAND = "COMMAND";
    public static final String COMMAND_OPEN = "COMMAND_OPEN";
    public static final String COMMAND_CLOSE = "COMMAND_CLOSE";

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        System.out.println("guo------开开开开开开开开");
    }

    @Override
    protected void onAbilityConnected() {
        System.out.println("guo------开3");
        super.onAbilityConnected();
    }

    @Override
    protected boolean onGesture(int gestureId) {
        System.out.println("guo------2");
        return super.onGesture(gestureId);
    }

    @Override
    protected boolean onKeyPressEvent(KeyEvent keyEvent) {
        System.out.println("guo------开1");
        return super.onKeyPressEvent(keyEvent);
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEventInfo accessibilityEventInfo) {
        System.out.println("guo------开");
        if (accessibilityEventInfo.getAccessibilityEventType() == AccessibilityEventInfo.WINDOWS_CHANGE_ACCESSIBILITY_FOCUSED) {
            CharSequence packageName = accessibilityEventInfo.getBundleName();
            CharSequence className = accessibilityEventInfo.getClassName();
            if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) {
                EventBus.getDefault().post(new ActivityChangedEvent(
                        accessibilityEventInfo.getBundleName().toString(),
                        accessibilityEventInfo.getClassName().toString()
                ));
                System.out.println("guo--------" + accessibilityEventInfo.getBundleName());
                System.out.println("guo--------" + accessibilityEventInfo.getClassName());

            }
        }
    }

    @Override
    public void onInterrupt() {

    }

    public static class ActivityChangedEvent {
        private final String mPackageName;
        private final String mClassName;

        public ActivityChangedEvent(String packageName, String className) {
            mPackageName = packageName;
            mClassName = className;
        }

        public String getPackageName() {
            return mPackageName;
        }

        public String getClassName() {
            return mClassName;
        }
    }
}